package com.java110.mall.chain.cmd.partner;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.partnerAdapt.PartnerGoodsDto;
import com.java110.dto.product.ProductDto;
import com.java110.intf.goods.IPartnerGoodsV1InnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 待同步的商品
 */
@Java110Cmd(serviceCode = "partner.queryWaitSyncGoods")
public class QueryWaitSyncGoodsCmd extends Cmd {

    @Autowired
    private IPartnerGoodsV1InnerServiceSMO partnerGoodsV1InnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        Assert.hasKeyAndValue(reqJson, "shopId", "shopId不能为空");
        Assert.hasKeyAndValue(reqJson, "ppId", "ppId不能为空");

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        PartnerGoodsDto partnerGoodsDto = BeanConvertUtil.covertBean(reqJson, PartnerGoodsDto.class);

        int count = partnerGoodsV1InnerServiceSMOImpl.queryWaitSyncGoodsCount(partnerGoodsDto);

        List<ProductDto> productDtos = null;

        if (count > 0) {
            productDtos = partnerGoodsV1InnerServiceSMOImpl.queryWaitSyncGoods(partnerGoodsDto);
        } else {
            productDtos = new ArrayList<>();
        }

        refrashProductCoverPhoto(productDtos);
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, productDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);
    }

    private void refrashProductCoverPhoto( List<ProductDto> productDtos) {

        if(ListUtil.isNull(productDtos)){
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (ProductDto tmpProductDto : productDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (ProductDto tmpProductDto : productDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }

                if(StringUtil.isEmpty(tmpFileRelDto.getFileRealName())){
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if(tmpFileRelDto.getFileRealName().startsWith("http")){
                    url =  tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }
        }
    }
}
